﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WM.MySqlTool
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        DBSchemaMgr _DBMgr = new DBSchemaMgr();

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.listBoxTables.Items.AddRange(_DBMgr.Tables.ToArray());
        }

        private void listBoxTables_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listBoxTables.SelectedIndex > -1)
            {
                this.chckListBoxColumn.Items.Clear();
                List<string> columns = _DBMgr.GetColumns(_DBMgr.Tables[listBoxTables.SelectedIndex]);
                this.chckListBoxColumn.Items.AddRange(columns.ToArray());
            }
            
        }
        /// <summary>
        /// 根据选中的表生成内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (listBoxTables.SelectedIndex > -1)
            {
                richTextBoxSql.Clear();
                string tableName = _DBMgr.Tables[listBoxTables.SelectedIndex];
                string content = _DBMgr.CreateTableClass(tableName);
                richTextBoxSql.Clear();
                richTextBoxSql.AppendText(content);
            }

        }

        private void btnAppend_Click(object sender, EventArgs e)
        {
            if (listBoxTables.SelectedItems.Count == 0)
                return;

            string tableName = _DBMgr.Tables[listBoxTables.SelectedIndex];
            string content = _DBMgr.CreateTableAppendSql(tableName);
            richTextBoxSql.Clear();
            richTextBoxSql.AppendText(content);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (listBoxTables.SelectedItems.Count == 0)
                return;

            string tableName = _DBMgr.Tables[listBoxTables.SelectedIndex];
            string content = _DBMgr.CreateTableUpdateSql(tableName);
            richTextBoxSql.Clear();
            richTextBoxSql.AppendText(content);
        }

        private void btnHTML_Click(object sender, EventArgs e)
        {
            if (listBoxTables.SelectedItems.Count == 0)
                return;

            string tableName = _DBMgr.Tables[listBoxTables.SelectedIndex];
            string content = _DBMgr.CreateTableHTML(tableName);

            richTextBoxSql.Clear();
            richTextBoxSql.AppendText(content);
        }

        private void btnSearchSql_Click(object sender, EventArgs e)
        {
            if (listBoxTables.SelectedItems.Count == 0)
                return;

            string tableName = _DBMgr.Tables[listBoxTables.SelectedIndex];
            string content = _DBMgr.CreateTableSqlSelect(tableName);
            richTextBoxSql.Clear();
            richTextBoxSql.AppendText(content);
        }
    }
}
